package com.edu.koTA07.takingOrder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlSerializer;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import com.edu.koTA07.XmlDataSet.BarangXmlDataSet;
import com.edu.koTA07.bean.CustomerBean;
import com.edu.koTA07.bean.PesananDetailBean;
import com.edu.koTA07.xmlreader.BarangXMLtoDataSet;

public class XmlFileCreatorCustomer{
	
    public XmlFileCreatorCustomer(CustomerBean customer) {
       // super.onCreate(savedInstanceState);
        //setContentView(R.layout.home);
        //create a new file called "new.xml" in the SD card
        File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/newCustomer.xml");
        try{
        	newxmlfile.createNewFile();
        }catch(IOException e){
        	Log.e("IOException", "exception in createNewFile() method");
        }
        //we have to bind the new file with a FileOutputStream
        FileOutputStream fileos = null;    
       
		
        try{
        	fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
        	Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        
        //we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try {
        	//we set the FileOutputStream as output for the serializer, using UTF-8 encoding
			serializer.setOutput(fileos, "UTF-8");
			//Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null) 
			serializer.startDocument(null, Boolean.valueOf(true)); 
			//set indentation option
			//serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 
			//start a tag called "root"
			/*serializer.startTag(null, "root"); 
			//i indent code just to have a view similar to xml-tree
				serializer.startTag(null, "child1");
				serializer.endTag(null, "child1");
				
				serializer.startTag(null, "child2");
				//set an attribute called "attribute" with a "value" for <child2>
				serializer.attribute(null, "attribute", "value");
				serializer.endTag(null, "child2");
			
				serializer.startTag(null, "child3");
				//write some text inside <child3>
				serializer.text("some text inside child3");
				serializer.endTag(null, "child3");
		
			serializer.endTag(null, "root");*/
				serializer.startTag(null, "ListCustomer"); 
				//i indent code just to have a view similar to xml-tree
					serializer.startTag(null, "Customer");
						serializer.startTag(null, "id_customer");
						//write some text inside <child3>
						serializer.text(customer.getIDCustomer());
						serializer.endTag(null, "id_customer");
						serializer.startTag(null, "nama_toko");
						//write some text inside <child3>
						serializer.text(customer.getNamaToko());
						serializer.endTag(null, "nama_toko");
						serializer.startTag(null, "nama_pemilik");
						//write some text inside <child3>
						serializer.text(customer.getNamaPemilik());
						serializer.endTag(null, "nama_pemilik");
						serializer.startTag(null, "telepon");
						//write some text inside <child3>
						serializer.text(""+customer.getTelepon());
						serializer.endTag(null, "telepon");
						serializer.startTag(null, "tipe_hari_kunjungan");
						//write some text inside <child3>
						serializer.text(customer.getTipeHariKunjungan());
						serializer.endTag(null, "tipe_hari_kunjungan");
						serializer.startTag(null, "kode_kunjungan");
						//write some text inside <child3>
						serializer.text(""+customer.getKodeKunjungan());
						serializer.endTag(null, "kode_kunjungan");
						serializer.startTag(null, "alamat_customer");
						//write some text inside <child3>
						serializer.text(""+customer.getAlamatCustomer());
						serializer.endTag(null, "alamat_customer");
						serializer.startTag(null, "nama_penghandle");
						//write some text inside <child3>
						serializer.text("");
						serializer.endTag(null, "nama_penghandle");
					serializer.endTag(null, "Customer");
				serializer.endTag(null, "ListCustomer");
				serializer.endDocument();
				//write xml data into the FileOutputStream
				serializer.flush();
			//finally we close the file stream
			fileos.close();
			/*try {
				InputStream ff = new FileInputStream(Environment.getExternalStorageDirectory()+"/new.xml");
				BarangXMLtoDataSet bb = new BarangXMLtoDataSet(ff);
				barang = bb.getBarangList();
	        } catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}*/
	        //TextView tv = (TextView)this.findViewById(R.id.result);
			//tv.setText("file has been created on SD card"+barang.getNamaBarang().get(0));
		} catch (Exception e) {
			Log.e("Exception","error occurred while creating xml file");
		}
    }
}
